<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button onclick="setCookie()">存cookie</button>
  <button onclick="getCookie()">取cookie</button>
  <script>
    function setCookie () {
      // cookie默认是会话有效，也就是说浏览器一旦关闭cookie就销毁了
      // 但是可以通过expires来设置过期时间
      var date = new Date()
      // 把日期调到3天以后
      date.setDate(date.getDate() + 3)
      // toUTCString转换为标准时区
      // document.cookie = 'name=zhangsan'
      document.cookie = `name=zhangsan;expires=${date.toUTCString()}`
    }

    function getCookie () {
      var str = document.cookie
      console.log(str)
    }
  </script>
</body>
</html>